#include "types/reflex.h"

namespace
{
std::unordered_map<std::string, reflex::task_type> g_task_type_map;
}

namespace reflex
{
TaskReflexManager::TaskReflexManager()
{

}
bool TaskReflexManager::isTypeRegistered(const std::string &type_name)
{
    if (type_name.empty())
    {
        return false;
    }
    return g_task_type_map.find(type_name) != g_task_type_map.end();
}
task::AbstractTaskConstPtr TaskReflexManager::constructTask(const std::string &type_name, const std::string &task_name)
{
    if (!isTypeRegistered(type_name))
    {
        throw TypeTraitsException(std::string("type [") + type_name + "] must be registered before used");
    }
    return g_task_type_map.at(type_name)(task_name);
}
void TaskReflexManager::saveType(const std::string &type_name, const task_type &type_construction)
{
    if (!type_name.empty())
    {
        g_task_type_map.emplace(type_name, type_construction);
    }
}
void TaskReflexManager::removeType(const std::string &type_name)
{
    if (!type_name.empty())
    {
        g_task_type_map.erase(type_name);
    }
}
}
